package top.maof.book.vo.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.maof.book.bo.ContentsBo;
import top.maof.book.model.Book;
import top.maof.book.model.Contents;
import top.maof.book.model.Web;
import top.maof.book.search.SearchPool;
import top.maof.book.service.BookService;
import top.maof.book.service.ContentsService;
import top.maof.book.service.WebService;
import top.maof.book.spider.runtime.BookSpider;
import top.maof.book.spider.runtime.Pair;
import top.maof.book.vo.ContentsVo;

import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.CountDownLatch;

@Service
@Slf4j
public class ContentsVoService {
    @Autowired
    BookService bookService;

    @Autowired
    ContentsService contentsService;

    @Autowired
    WebService webService;

    @Autowired
    BookSpider bookSpider;

    @Deprecated
    public ContentsVo contentsVo(int bookId) throws Exception {
        ContentsVo contentsVo = new ContentsVo();
        Book book = bookService.find(bookId);
        if (book == null) return null;
        contentsVo.setBook(book);
        List<Contents> list = contentsService.findByBookId(bookId);
        Set<Contents> set = new TreeSet<>(list);
        CountDownLatch countDownLatch;
        if (set != null && set.size() > 0) {
            countDownLatch = new CountDownLatch(1);
            for (Contents contents : set) {
                // log.info(contents.toString());
                Web web = webService.find(contents.getWebId());
                ContentsBo contentsBo = new ContentsBo();
                contentsBo.setWeb(web);
                // 若内容已保存,直接解析;未保存,直接抓取
                if (contents.getContent() == null || "".equals(contents.getContent().trim())) {
                    SearchPool.execute(() -> {
                        try {
                            contents.setContent(bookSpider.content(contents, web));
                            if (!"[]".equals(contents.getContent()) && contents.getContent() != null && !"".equals(contents.getContent())) {
                                // 书已完本,序列化内容并保存
                                if (book.getState().contains("完本")) {
                                    Contents c = new Contents();
                                    c.setId(contents.getId());
                                    c.setContent(contents.getContent());
                                    contentsService.updateByPrimaryKeySelective(c);
                                }
                                countDownLatch.countDown();
                            }
                        } catch (Exception e) {
                            log.info(e.getMessage());
                        }
                    });
                } else {
                    countDownLatch.countDown();
                }
                contentsBo.setContents(contents);
                contentsVo.add(contentsBo);
            }
            if (countDownLatch != null)
                countDownLatch.await();
        }

        return contentsVo;
    }

    @Deprecated
    public Set contentsSet(int bookId, int webId) throws Exception {
        Set<Pair> set = null;
        List<Contents> list = contentsService.findByBookIdAndWebId(bookId, webId);
        if (list != null && list.size() > 0) {
            String url = list.get(0).getUrl();
            Web web = webService.find(list.get(0).getWebId());
            //若内容已保存,直接解析;未保存,直接抓取
            if (list.get(0).getContent() != null && !"".equals(list.get(0).getContent().trim()))
                set = bookSpider.contents(list.get(0).getContent(), web.getUrl(), web.getContentsSelect());
            else
                set = bookSpider.contents(url, web);
        }
        return set;
    }
}
